from myelin.Ns import MYELIN, MYELINS

from myelin.server.nodetype.NodeType import NodeType
from nodetypes.Ns import NODETYPE_NS
from nodetypes.models.Resource import Resource as ResourceModel

import view

class Resource(NodeType):
    
    typeInfo = {
        'key'           : u'resource',
        'name'          : u'Resource',
        'namespace-uri' : NODETYPE_NS,
    }
    
    ACTIONS = {
        'view'          : view.TRANSFORMS,
    }
    
            
    def createDomainOptions(self, context, node, selected=None):
        domains = context.account.domains
        count = len(domains)
        if count == 0:
            node.unavailable = u'1'
        else:            
            opts = node.xml_append(
                node.xml_create_element(
                    u'a:options', MYELINS,
                    attributes = {
                        u'count'    : unicode(count),
                    }
                )
            )
            oxa = opts.xml_append
            oxc = opts.xml_create_element
            for k, v in domains.items():
                if k == selected:
                    node.selected = k
                oxa(
                    oxc(
                        u'a:option', MYELINS,
                        attributes = {
                            u'key'      : k,
                            u'label'    : v[0],
                        }
                    )
                )
        return node
        

NODETYPES = {
    MYELIN + 'Resource' : Resource,
}


    